<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="企业的Id" prop="entId">
      <el-input v-model="dataForm.entId" placeholder="企业的Id"></el-input>
    </el-form-item>
    <el-form-item label="商品批次号" prop="commodityBatch">
      <el-input v-model="dataForm.commodityBatch" placeholder="商品批次号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="commodityName">
      <el-input v-model="dataForm.commodityName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="租仓id" prop="oldStorageId">
      <el-input v-model="dataForm.oldStorageId" placeholder="租仓id"></el-input>
    </el-form-item>
    <el-form-item label="租仓名称" prop="oldStorageName">
      <el-input v-model="dataForm.oldStorageName" placeholder="租仓名称"></el-input>
    </el-form-item>
    <el-form-item label="库位id" prop="oldSpaceId">
      <el-input v-model="dataForm.oldSpaceId" placeholder="库位id"></el-input>
    </el-form-item>
    <el-form-item label="库位名称" prop="oldSpaceName">
      <el-input v-model="dataForm.oldSpaceName" placeholder="库位名称"></el-input>
    </el-form-item>
    <el-form-item label="租仓id" prop="newStorageId">
      <el-input v-model="dataForm.newStorageId" placeholder="租仓id"></el-input>
    </el-form-item>
    <el-form-item label="租仓名称" prop="newStorageName">
      <el-input v-model="dataForm.newStorageName" placeholder="租仓名称"></el-input>
    </el-form-item>
    <el-form-item label="库位id" prop="newSpaceId">
      <el-input v-model="dataForm.newSpaceId" placeholder="库位id"></el-input>
    </el-form-item>
    <el-form-item label="库位名称" prop="newSpaceName">
      <el-input v-model="dataForm.newSpaceName" placeholder="库位名称"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="commodityNum">
      <el-input v-model="dataForm.commodityNum" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="0 未删 1已删" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="0 未删 1已删"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          entId: '',
          commodityBatch: '',
          commodityName: '',
          oldStorageId: '',
          oldStorageName: '',
          oldSpaceId: '',
          oldSpaceName: '',
          newStorageId: '',
          newStorageName: '',
          newSpaceId: '',
          newSpaceName: '',
          commodityNum: '',
          area: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          entId: [
            { required: true, message: '企业的Id不能为空', trigger: 'blur' }
          ],
          commodityBatch: [
            { required: true, message: '商品批次号不能为空', trigger: 'blur' }
          ],
          commodityName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          oldStorageId: [
            { required: true, message: '租仓id不能为空', trigger: 'blur' }
          ],
          oldStorageName: [
            { required: true, message: '租仓名称不能为空', trigger: 'blur' }
          ],
          oldSpaceId: [
            { required: true, message: '库位id不能为空', trigger: 'blur' }
          ],
          oldSpaceName: [
            { required: true, message: '库位名称不能为空', trigger: 'blur' }
          ],
          newStorageId: [
            { required: true, message: '租仓id不能为空', trigger: 'blur' }
          ],
          newStorageName: [
            { required: true, message: '租仓名称不能为空', trigger: 'blur' }
          ],
          newSpaceId: [
            { required: true, message: '库位id不能为空', trigger: 'blur' }
          ],
          newSpaceName: [
            { required: true, message: '库位名称不能为空', trigger: 'blur' }
          ],
          commodityNum: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '0 未删 1已删不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entallocation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.entId = data.entAllocation.entId
                this.dataForm.commodityBatch = data.entAllocation.commodityBatch
                this.dataForm.commodityName = data.entAllocation.commodityName
                this.dataForm.oldStorageId = data.entAllocation.oldStorageId
                this.dataForm.oldStorageName = data.entAllocation.oldStorageName
                this.dataForm.oldSpaceId = data.entAllocation.oldSpaceId
                this.dataForm.oldSpaceName = data.entAllocation.oldSpaceName
                this.dataForm.newStorageId = data.entAllocation.newStorageId
                this.dataForm.newStorageName = data.entAllocation.newStorageName
                this.dataForm.newSpaceId = data.entAllocation.newSpaceId
                this.dataForm.newSpaceName = data.entAllocation.newSpaceName
                this.dataForm.commodityNum = data.entAllocation.commodityNum
                this.dataForm.area = data.entAllocation.area
                this.dataForm.delFlag = data.entAllocation.delFlag
                this.dataForm.createBy = data.entAllocation.createBy
                this.dataForm.createTime = data.entAllocation.createTime
                this.dataForm.updateBy = data.entAllocation.updateBy
                this.dataForm.updateTime = data.entAllocation.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entallocation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'entId': this.dataForm.entId,
                'commodityBatch': this.dataForm.commodityBatch,
                'commodityName': this.dataForm.commodityName,
                'oldStorageId': this.dataForm.oldStorageId,
                'oldStorageName': this.dataForm.oldStorageName,
                'oldSpaceId': this.dataForm.oldSpaceId,
                'oldSpaceName': this.dataForm.oldSpaceName,
                'newStorageId': this.dataForm.newStorageId,
                'newStorageName': this.dataForm.newStorageName,
                'newSpaceId': this.dataForm.newSpaceId,
                'newSpaceName': this.dataForm.newSpaceName,
                'commodityNum': this.dataForm.commodityNum,
                'area': this.dataForm.area,
                'delFlag': this.dataForm.delFlag,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
